/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WeightsDialog.java
 *
 * Created on Jun 6, 2010, 4:27:35 PM
 */

package org.electre.ui;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.electre.core.Atributo;

/**
 *
 * @author william
 */
public class WeightsDialog extends javax.swing.JDialog {
    private final List<Atributo> atributos;
    private boolean edited;

    /** Creates new form WeightsDialog */
    public WeightsDialog(java.awt.Frame parent, boolean modal, List<Atributo> atributos) {
        super(parent, modal);
        initComponents();
        this.atributos = atributos;
        this.jPanel2.setLayout(new GridBagLayout());
        this.populatePanel();
        this.updateTotal();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(org.openide.util.NbBundle.getMessage(WeightsDialog.class, "WeightsDialog.title")); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(WeightsDialog.class, "WeightsDialog.jPanel1.border.title"))); // NOI18N
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));

        jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 346, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 273, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jPanel2);

        jLabel1.setText(org.openide.util.NbBundle.getMessage(WeightsDialog.class, "WeightsDialog.jLabel1.text")); // NOI18N

        jLabel2.setText(org.openide.util.NbBundle.getMessage(WeightsDialog.class, "WeightsDialog.jLabel2.text")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(57, 57, 57)
                        .addComponent(jLabel2)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jButton1.setText(org.openide.util.NbBundle.getMessage(WeightsDialog.class, "WeightsDialog.jButton1.text")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(org.openide.util.NbBundle.getMessage(WeightsDialog.class, "WeightsDialog.jButton2.text")); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
       /*
        BigDecimal total = this.updateTotal();
        total = total.setScale(4,BigDecimal.ROUND_DOWN);
        if (total.doubleValue() == 1d) {
        *
        */
            this.updateAtributos();
            this.edited = true;
            this.dispose();
        /*}else{
            JOptionPane.showMessageDialog(this, "La suma de las ponderaciones debe ser de 1", "Error",0);
        }
         * 
         */
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.edited = false;
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    
    private void updateAtributos() {
        int j = 0;
        for (int i = 0; i < this.atributos.size(); i++) {
            this.atributos.get(i).setNombre(((JTextField)jPanel2.getComponent(j)).getText());
            this.atributos.get(i).setPeso((Double)((JSpinner)jPanel2.getComponent(j+2)).getValue());
            int comboIndex = ((JComboBox)jPanel2.getComponent(j+1)).getSelectedIndex();
            this.atributos.get(i).setPositivo((comboIndex == 0)?true:false);
            j += 3;
        }
    }
    private void populatePanel() {
        for(Atributo atributo : this.atributos) {
            this.addComponents(atributo.getPeso(),(atributo.isPositivo())?0:1, atributo.getNombre());
        }
        this.jPanel2.revalidate();
        this.jPanel2.repaint();
    }
    private void addComponents(double weight, int comboIndex, String name) {
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridy = this.jPanel2.getComponentCount() / 2;
        c.gridx = 0;
        c.insets = new Insets(2,2,2,2);
        JTextField textField = new JTextField(20);
        c.fill = GridBagConstraints.HORIZONTAL;
        textField.setText(name);
        this.jPanel2.add(textField, c);
        c.gridx = 1;
       // DefaultComboBoxModel model = new DefaultComboBoxModel();
        JComboBox combo = new JComboBox(new String[]{"mayor es mejor","menor es mejor"});
        combo.setSelectedIndex(comboIndex);
        this.jPanel2.add(combo, c);
        c.gridx = 2;
        JSpinner spinner = new JSpinner();
        spinner.setModel(new javax.swing.SpinnerNumberModel(Double.valueOf(0.1), Double.valueOf(0.01), Double.valueOf(1), Double.valueOf(0.01)));
        spinner.setValue(weight);
        spinner.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                WeightsDialog.this.updateTotal();
            }

        });
        this.jPanel2.add(spinner, c);
    }

    private BigDecimal updateTotal() {
        BigDecimal aux = BigDecimal.ZERO;
         for (Component c : jPanel2.getComponents()) {
                if (c instanceof JSpinner) {
                    aux = aux.add(new BigDecimal((Double)((JSpinner)c).getValue()).setScale(4,BigDecimal.ROUND_DOWN));
                }
        }
        DecimalFormat cf = new DecimalFormat("#.####");
        this.jLabel2.setText(cf.format(aux));
        return aux;
    }
    public List<Atributo> getAtributos() {
        return atributos;
    }

    public boolean isEdited() {
        return edited;
    }

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables



}
